<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item>
                    <!--                                        <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>-->
                    <!--                                        <el-button type="success" @click="handleReset" class="reset-button">重置</el-button>-->
                    <el-button type="primary" @click="showCreateDialog" class="new-button">添加
                    </el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%" row-key="uid"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      
                <el-table-column prop="columnName" align="left" label="栏目名称" />
                <!-- <el-table-column prop="menuIcon" align="center" label="图标">
                  <template slot-scope="scope">
                  </template>
                </el-table-column> -->
                <el-table-column prop="routerAddress" align="center" label="路由地址" />
                <el-table-column prop="componentPath" align="center" label="组件路径" />
                <el-table-column prop="sortNum" align="center" label="菜单排序" />
                <!-- <el-table-column prop="" align="center" label="是否网站展示" /> -->
                <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text" class="table-edit-btn"
                      @click="showEditDialog(scope.$index, scope.row)"></el-button>
                    <!--编辑-->
                    <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button><!--删除-->
                  </template>
                </el-table-column>
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="60%">
      <el-form label-width="120px" ref="form" :model="form" :rules="formRules">
        <el-form-item label="栏目名称" prop="columnName">
          <el-input v-model="form.columnName" />
        </el-form-item>
        <el-form-item label="栏目等级" prop="columnGrade">
          <el-select style="width: 100%;" v-model="form.columnGrade">
            <el-option v-for="item in columnGradeList" :value="item" :label="item" :key="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="父级栏目名称" prop="pname">
          <el-input v-model="form.pname" disabled>
            <template slot="append">
              <el-button icon="el-icon-search" @click="showParentColumnDialog"></el-button>
            </template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item label="菜单图标" prop="menuIcon">
          <x-upload v-model="form.menuIcon" />
        </el-form-item> -->
        <el-form-item label="路由地址" prop="routerAddress">
          <el-input v-model="form.routerAddress" />
        </el-form-item>
        <el-form-item label="组件路径" prop="componentPath">
          <el-input v-model="form.componentPath" />
        </el-form-item>
        <el-form-item label="排序" prop="sortNum">
          <el-input v-model="form.sortNum" type="number" />
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>

    <el-dialog title="选择父级栏目" :visible.sync="parentColumnDialogVisible">
      <el-table :data="tableData" :stripe="true" style="width: 100%" row-key="uid" :tree-props="{ children: 'childs', }">

        <el-table-column prop="columnName" align="center" label="栏目名称" />
        <!-- <el-table-column prop="description" align="center" label="图标" /> -->
        <el-table-column prop="routerAddress" align="center" label="路由地址" />
        <el-table-column prop="componentPath" align="center" label="组件路径" />
        <el-table-column prop="sortNum" align="center" label="菜单排序" />
        <!-- <el-table-column prop="description" align="center" label="是否网站展示" /> -->
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="onParentColumnSelected(scope.row)">选择</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import * as columnInfo from "../../api/columnInfo"
import * as formUtils from "../../utils/formUtils";
import XUpload from "../../components/xupload/XUpload";

export default {
  name: "list",
  components: { XUpload, XPagination },
  data () {
    return {
      searchForm: {
        roleName: ""
      },
      tableData: [],
      loading: false,
      dialogVisible: false,
      total: 0,
      dialogTitle: "添加栏目",
      columnGradeList: ['一级', '二级'],
      form: {
        uid: "",
        columnName: "",
        columnGrade: "",
        pname: "",
        puid: "0",
        menuIcon: "",
        routerAddress: "",
        componentPath: "",
        sortNum: "",
      },
      editStatus: "add",
      formRules: {
        columnName: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        columnGrade: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        // pname: [
        //     { required: true, message: '不能为空', trigger: 'blur' },
        // ],
        // menuIcon: [
        //     { required: true, message: '不能为空', trigger: 'blur' },
        // ],
        routerAddress: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        componentPath: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
        sortNum: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
      },
      parentColumnDialogVisible: false,
      parentColumnTableData: [],
      currentSelectedParentColumnItem: {}


    }
  },
  methods: {
    getTreeList () {
      const param = {
        ...this.searchForm,
      }
      columnInfo.queryTree(param).then(response => {
        console.log(response)
        this.tableData = response.data.data
        this.tableData.sort((a, b) => a.sortNum - b.sortNum);
        this.tableData.forEach(node => {
          if (node.children.length > 0) {
            node.children.sort((a, b) => a.sortNum - b.sortNum); // 递归对子节点进行排序
          }
        });
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          uid: data.uid,
        }
        this.loading = true
        columnInfo.deleteById(param).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    handleSearch () {
      this.getTreeList()
    },
    showCreateDialog () {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.dialogTitle = "添加栏目"
        this.editStatus = "add"
        this.$refs.form.resetFields()
        this.form = Object.assign({}, this.$options.data().form)
      })
    },
    handleSave () {
      if (this.editStatus === "add") {
        this.handleAdd()
      } else {
        this.handleEdit()
      }
    },
    handleAdd () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        columnInfo.insert(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })

      })
    },
    handleEdit () {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return false
        }
        const param = Object.assign({}, this.form)
        formUtils.filterFormFields(param)
        columnInfo.update(param).then(result => {
          this.$message.success(result.data.msg)
          this.dialogVisible = false
          this.handleSearch()
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      })
    },
    showEditDialog (index, data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        this.form = Object.assign({}, data)
        this.dialogTitle = "编辑栏目"
        this.editStatus = "edit"
      })
    },
    showParentColumnDialog () {
      this.parentColumnDialogVisible = true
      this.getParentColumnTableData()
    },
    getParentColumnTableData () {
      const param = {}
      columnInfo.queryTree(param).then(response => {
        console.log(response)
        this.parentColumnTableData = response.data.data
      }).catch(error => {
        console.log(error)
      })
    },
    onParentColumnSelected (data) {
      // this.currentSelectedParentColumnItem = data
      this.form.pname = data.columnName
      this.form.puid = data.uid
      this.parentColumnDialogVisible = false
    }
  },
  mounted () {
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
